<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>课程管理</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
    <style>
        .main-container {
            padding: 20px;
        }
        .top-bar {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="layui-layout layui-layout-admin">
        <!-- 顶部导航栏 -->
        <div class="layui-header">
            <div class="layui-logo">作业管理系统</div>
            <ul class="layui-nav layui-layout-left">
                <li class="layui-nav-item"><a href="${pageContext.request.contextPath}/admin/students">学生管理</a></li>
                <li class="layui-nav-item"><a href="${pageContext.request.contextPath}/admin/teachers">教师管理</a></li>
                <li class="layui-nav-item layui-this"><a href="${pageContext.request.contextPath}/admin/courses">课程管理</a></li>
            </ul>
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item">
                    <a href="javascript:;">
                        <i class="layui-icon layui-icon-username"></i>
                        管理员
                    </a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" onclick="logout()">退出登录</a></dd>
                    </dl>
                </li>
            </ul>
        </div>

        <!-- 内容主体区域 -->
        <div class="layui-body">
            <div class="main-container">
                <div class="top-bar">
                    <button class="layui-btn" onclick="addCourse()">
                        <i class="layui-icon layui-icon-add-1"></i> 添加课程
                    </button>
                </div>
                
                <table class="layui-table">
                    <thead>
                        <tr>
                            <th>课程号</th>
                            <th>课程名称</th>
                            <th>学分</th>
                            <th>授课教师</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach items="${courses}" var="course">
                            <tr>
                                <td>${course.cno}</td>
                                <td>${course.cname}</td>
                                <td>${course.credit}</td>
                                <td>${course.tname}</td>
                                <td>
                                    <button class="layui-btn layui-btn-xs" onclick="editCourse('${course.cno}')">
                                        <i class="layui-icon layui-icon-edit"></i>编辑
                                    </button>
                                    <button class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteCourse('${course.cno}')">
                                        <i class="layui-icon layui-icon-delete"></i>删除
                                    </button>
                                </td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        layui.use(['element', 'layer'], function(){
            var element = layui.element;
            var layer = layui.layer;
        });
        
        function addCourse() {
            layer.open({
                type: 2,
                title: '添加课程',
                area: ['500px', '400px'],
                content: '${pageContext.request.contextPath}/admin/course/add'
            });
        }
        
        function editCourse(cno) {
            layer.open({
                type: 2,
                title: '编辑课程',
                area: ['500px', '400px'],
                content: '${pageContext.request.contextPath}/admin/course/edit?cno=' + cno
            });
        }
        
        function deleteCourse(cno) {
            layer.confirm('确定要删除这个课程吗？', {icon: 3, title:'提示'}, function(index){
                $.ajax({
                    url: '${pageContext.request.contextPath}/admin/course/delete',
                    type: 'POST',
                    data: {cno: cno},
                    success: function(res){
                        if(res.code === 0){
                            layer.msg('删除成功', {icon: 1});
                            location.reload();
                        } else {
                            layer.msg(res.msg || '删除失败', {icon: 2});
                        }
                    },
                    error: function(){
                        layer.msg('系统错误', {icon: 2});
                    }
                });
                layer.close(index);
            });
        }
        
        function logout() {
            layer.confirm('确定要退出登录吗？', {icon: 3, title:'提示'}, function(index){
                window.location.href = '${pageContext.request.contextPath}/logout';
                layer.close(index);
            });
        }
    </script>
</body>
</html> 